package com.liangzhong.proxy.ladderMatch
{
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.ladderMatch.SMDLadderMatch;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.MatchType;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.ladderMatch.LadderMatchConst;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;
	
	public class LadderMatchProxy extends BaseProxy
	{
		public static const PROXYNAME:String = "LadderMatchProxy__proxyName";
		public function LadderMatchProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
		}
		/**接受 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case MainCommandType.MATCH:
					return onDeal(subCmdID,buffer,dataSize);
				case MainCommandType.MATCH_RESULE:
					return ladderMatchFinish(subCmdID,buffer,dataSize);
				case MainCommandType.PUBLICCOMMANDTYPE:
					return publicDeal(subCmdID,buffer,dataSize);
					
					 
			}
			return false;
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDLadderMatch.CANCEL_MATCH:
					getCancelMatch(buffer);
					return true;
				case SMDLadderMatch.MATCH_STATUS:
					matchstatus(buffer);
					return true;
				case SMDLadderMatch.ADD_FIGHT:
					getAddFightTime(buffer);
					return true;
			}
			return false;
		}
		/**
		 *一场比赛完成后 
		 * @param buffer
		 * 
		 */		
		private function ladderMatchFinish(subCmdID:uint,
										   buffer:ByteArray, 
										   dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDLadderMatch.LADDER_RESULT:
					var retcode :int = buffer.readInt();
					if(retcode == 0)
					{
						AppData.getInstance().ladderDataManger.ladderInfovo.honour = buffer.readInt();
						AppData.getInstance().ladderDataManger.ladderInfovo.ladderintegral = buffer.readInt();
						//					AppData.getInstance().ladderDataManger.ladderInfovo.ladderrank = buffer.readInt();
//						AppData.getInstance().ladderDataManger.ladderInfovo.ladderlevel = buffer.readByte();
						this.sendNotification(LadderMatchConst.GET_ADD_FIGHT_TIME,{status:true});
					}
					return true;
//				case SMDLadderMatch
			}
			return false
			
//			requestTopList();
		}
		/**
		 * 处理40协议 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function publicDeal(subCmdID:uint,
									buffer:ByteArray, 
									dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDLadderMatch.EXCHANGE:
					getExchnage(buffer);
					return true;
			}
			return false;
		}
		/**
		 *比赛状态 
		 * * type
		 * 1.自动报名
		 * 2.取消自动报名
		 * 3.手动报名
		 * 4.手动取消报名 
		 * @param $obj
		 * 
		 */
		public function matchStatuc($obj:Object):void
		{
			if($obj.type == 4)
			{
				cancelMatch();
			}else
			{
				var sendBuf :ByteArray = 
					CMD_Head.getHeadByteArrayBuff(CMD_Head.Size+2,MainCommandType.MATCH,SMDLadderMatch.MATCH_STATUS);
				sendBuf.writeByte($obj.ballt);
				sendBuf.writeByte($obj.type);
				sendBuf.position = 0;
				clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
			}
		}
		/**
		 *比赛状态  
		 * @param buffer
		 * 1.自动报名
		 * 2.取消自动报名
		 * 3.手动报名
		 */
		private function matchstatus(buffer:ByteArray):void
		{
			var retcode :int = buffer.readInt();
			if(retcode == 0)
			{
//				var formation:int = buffer.readByte();
//				var type:int = buffer.readByte();//类型1.自动报名 2.取消自动报名 3.手动报名
//				if(type == 1)
//				{
//					AppData.getInstance().ladderDataManger.ladderInfovo.isAutoMatch = true;
//				}else
//				{
//					AppData.getInstance().ladderDataManger.ladderInfovo.isAutoMatch = false;
//				}
				AppData.getInstance().ladderDataManger.ladderInfovo.isApplyStatus = true;
//				AppData.getInstance().ladderDataManger.ladderInfovo.usebattle = formation;
				this.sendNotification(LadderMatchConst.GET_MATCH_STATUS,{type:3,status:true});
				Alert.showErrorTip("报名成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode));
				this.sendNotification(LadderMatchConst.GET_MATCH_STATUS,{type:3,status:false});
			}
		}
		/**
		 *手动取消比赛 
		 * 
		 */
		private function cancelMatch():void
		{
//			trace("请求取消天梯比赛");
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,MainCommandType.MATCH,SMDLadderMatch.CANCEL_MATCH);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
			
		}
		/**
		 *返回手动取消比赛 
		 * 
		 */
		private function getCancelMatch(buffer:ByteArray):void
		{
//			trace("返回取消天梯比赛");
			
			var retcode :int = buffer.readInt();
			if(retcode == 0)
			{
//				var time:int = buffer.readShort();
//				AppData.getInstance().ladderDataManger.ladderInfovo.isAutoMatch = false;
				AppData.getInstance().ladderDataManger.ladderInfovo.isApplyStatus = false;
				this.sendNotification(LadderMatchConst.GET_MATCH_STATUS,{type:4,status:true});
				Alert.showErrorTip("成功取消比赛");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode));
				this.sendNotification(LadderMatchConst.GET_MATCH_STATUS,{type:4,status:false});
			}
		}
		/**
		 * 报名列表 
		 * @param $obj
		 * 
		 */
		public function applyList($obj:Object):void
		{
//			AppData.getInstance().ladderDataManger.clearApplyist();
//			var sendBuf :ByteArray = 
//				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,MainCommandType.MATCH,SMDLadderMatch.JOIN_MATCH);
//			sendBuf.position = 0;
//			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.APPLYLIST;
			variables.page = $obj.page;
			ProxyUtils.instance.webClient(getApplyList,variables,false);
		}
		/**
		 * 返回报名列表 
		 * @param buffer
		 * 
		 */
		private function getApplyList($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,LadderMatchProxy);
			if(obj.retcode == 0)
			{
				var arr:Array = AppData.getInstance().ladderDataManger.getapplyList(obj.TTPlayerList);
				this.sendNotification(LadderMatchConst.GET_APPLYLIST,{arr:arr,total:obj.total});
			}else
			{
				//Alert.showErrorTip(ErrorTips.instance.getCErrorTip(obj.retcode));
				
			}
		}
		/**
		 * 请求前十排名  
		 * 
		 */
		public function requestTopList():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.TOP_10_LIST;
			ProxyUtils.instance.webClient(getTopList,variables,false);
		}
		/**
		 *返回前十 
		 * @param $data
		 * 
		 */		
		private function getTopList($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,LadderMatchProxy);
			if(obj.retcode == 0)
			{
				AppData.getInstance().ladderDataManger.gettopList(obj);
				this.sendNotification(LadderMatchConst.GET_TOP_LIST);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *增加挑战次数 
		 * 
		 */
		public function requestAddFightTime():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,MainCommandType.MATCH,SMDLadderMatch.ADD_FIGHT);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回增加挑战次数 
		 * @param buffer
		 * 
		 */
		private function getAddFightTime(buffer:ByteArray):void
		{
			var retcode :int = buffer.readInt();
			if(retcode == 0)
			{
				var time:int = buffer.readShort();
				AppData.getInstance().ladderDataManger.ladderInfovo.laddercount = time;
				this.sendNotification(LadderMatchConst.GET_ADD_FIGHT_TIME,{status:true});
				Alert.showErrorTip("成功购买");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode));
				this.sendNotification(LadderMatchConst.GET_ADD_FIGHT_TIME,{status:false});
			}
		}
		/**
		 *请求更多的天梯排名  
		 * 
		 */
		public function requestMoreLadderRank():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.MORE_RANK;
			ProxyUtils.instance.webClient(getMoreLadderRank,variables,false);
		}
		/**
		 *返回更多的天梯排名  
		 * @param $data
		 * 
		 */
		private function getMoreLadderRank($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,LadderMatchProxy);
			if(obj.retcode == 0)
			{
				AppData.getInstance().ladderDataManger.analysyMoreRank(obj.moreSort);
				AppData.getInstance().ladderDataManger.updataMoreRankTime = AppData.getInstance().time;
				this.sendNotification(LadderMatchConst.GET_MORE_LADDER_RANK);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *请求兑换 
		 * @param $obj
		 * 
		 */
		public function requestExchange($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 7,MainCommandType.PUBLICCOMMANDTYPE,SMDLadderMatch.EXCHANGE);
			sendBuf.writeInt($obj.pid);
			sendBuf.writeByte($obj.type);
			sendBuf.writeShort($obj.num);
			
			
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回兑换 
		 * @param buffer
		 * 
		 */
		private function getExchnage(buffer:ByteArray):void
		{
			var retcode :int = buffer.readInt();
			if(retcode == 0)
			{
				var i:int = buffer.readInt();
				AppData.getInstance().ladderDataManger.ladderInfovo.honour = i;
				this.sendNotification(LadderMatchConst.GET_EXCHANGE,{status:true});
				Alert.showErrorTip("兑换成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode));
				this.sendNotification(LadderMatchConst.GET_EXCHANGE,{status:false});
			}
		}
		/**
		 *天梯比赛开始 
		 * 
		 */
		public function ladderMatchStart($obj:Object):void
		{
			if($obj.type == MatchType.LADDYMATCH)
			{
				if(!AppData.getInstance().ladderDataManger.ladderInfovo.isAutoMatch)
				{
					AppData.getInstance().ladderDataManger.ladderInfovo.isApplyStatus = false;
				}
				AppData.getInstance().ladderDataManger.ladderInfovo.laddercount--;
				this.sendNotification(LadderMatchConst.LADDERMATCH_START);
			}
		}
		/**
		 *天梯比赛结束 
		 * 
		 */
		public function ladderMatchEnd($obj:Object):void
		{
			if($obj.ntype == MatchType.LADDYMATCH)
			{
				this.sendNotification(LadderMatchConst.LADDERMATCH_END);
			}
		}
		
		
		
	}
}